<?php

class Odometer_no extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Odometer_no':
	 * @var integer $id
	 * @var string $plate_no
	 * @var double $eom_no
	 * @var string $odom_date
	 * @var integer $state
	 * @var string $create_time
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'odometer_no';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('plate_no','length','max'=>10),
			array('eom_no', 'numerical', 'integerOnly'=>true),
			array('plate_no,eom_no,odom_date', 'required'),
			array('odom_date','match','pattern' => '/\d{4}-\d{2}$/','message'=>'{attribute} 必须为日期型(年-月)'),
		);
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		return true;
	}
	
	protected function afterValidate($on){
		if($this->hasErrors()) return;
		$this->odom_date.='-01';
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引',
			'plate_no' => '车牌号',
			'eom_no' => '月末读数(公里)',
			'odom_date' => '月份',
			'state' => '状态',
			'create_time' => '创建时间',
		);
	}
}